import axios from '../utils/request';

class OrderApi {

    // 构造函数，初始化 axios 实例
    constructor() {
        this.axios = axios;
    }

    // 获取所有订单
    fetchOrders() {
        return this.axios.get('/api/orders/list') // 发送 GET 请求获取订单列表
            .then(response => {
            return response.data.data;
    })
    .catch(error => {
            console.error("获取订单时出错:", error);
        throw error;
    });
    }

    // 根据订单 ID 获取订单详情
    fetchOrderDetails(orderId) {
        return this.axios.get(`/api/orders/detail/${orderId}`)
            .then(response => {
            return response.data.data;
    })
    .catch(error => {
            console.error(`获取订单详情（ID: ${orderId}）时出错:`, error);
        throw error;
    });
    }

    // 创建新订单
    createOrder(orderData) {
        return this.axios.post('/api/orders/add', orderData)
            .then(response => {
            return response.data;
    })
    .catch(error => {
            console.error("创建订单时出错:", error);
        throw error;
    });
    }

    // 更新订单状态
    updateOrder(orderId, orderData) {
        return this.axios.put(`/api/orders/update/status/${orderId}`, orderData) // 发送 PUT 请求更新订单状态，传递订单数据
            .then(response => {
            return response.data;
    })
    .catch(error => {
            console.error(`更新订单（ID: ${orderId}）时出错:`, error);
        throw error;
    });
    }

    // 删除订单
    deleteOrder(orderId) {
        return this.axios.delete(`/api/orders/delete/${orderId}`)
            .then(response => {
            return response.data;
    })
    .catch(error => {
            console.error(`删除订单（ID: ${orderId}）时出错:`, error);
    });
    }

    // 导出订单数据
    exportOrders() {
        return this.axios.get('/api/orders/export')
            .then(response => {
            return response.data;
    })
    .catch(error => {
            console.error("导出订单时出错:", error);
        throw error;
    });
    }
}

export default OrderApi;
